package com.linewell.daoImpl;

import com.linewell.annotation.EncrptionAop;
import com.linewell.dao.BookDao;
import com.linewell.pojo.Book;
import com.linewell.util.UpdateUtils;
import com.mongodb.WriteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Title: demo
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/4/26
 */
@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    @EncrptionAop
    public Book insertBook(Book entity, String test, String tesets) {
        System.out.println(entity.getReader());
        mongoTemplate.insert(entity);
        return entity;
    }

    @Override
    public List<Book> findBook(Query query, Pageable pageable) {

        query.skip(pageable.getPageNumber());
        query.limit(pageable.getPageSize());
        return mongoTemplate.find(query, Book.class);
    }


    @Override
    public boolean updateBook(String id, Map map)  {

        Update update = UpdateUtils.getUpdateParameter(Book.class, map);
        Query query = Query.query(Criteria.where("id").is(id));
        WriteResult result = mongoTemplate.updateFirst(query, update, Book.class);
        return result.getN()>0?true:false;
    }

    @Override
    public boolean updateBook(Query query, Update update) {

        WriteResult ur = this.mongoTemplate.updateFirst(query, update, Book.class);
        return ur.getN()>0;
    }

    @Override
    public Long updates(Query query, Update update) {
        WriteResult wr = mongoTemplate.updateMulti(query, update, Book.class);
        return 0l;
    }

    @Override
    public Long deletes(Query query) {
        WriteResult wr = mongoTemplate.remove(query, Book.class);
        return 0l;
    }

    @Override
    public void saveBook(Book entity) {
        mongoTemplate.save(entity);
    }

    @Override
    public boolean updateBookAll(Query query, Update update) {
        WriteResult ur = this.mongoTemplate.updateMulti(query, update, Book.class);
        return ur.getN()>0;
    }

    @Override
    public void findBooks(Pageable pageable) {
        Query query = new Query();
        query.skip(pageable.getPageNumber());
        query.limit(pageable.getPageSize());
        List<Book> list = mongoTemplate.find(query,Book.class);
    }

    public void findBookByCon(Query query) {
        List<Book> book = mongoTemplate.find(query,Book.class);
        System.out.println();
    }

    public void findBookSort(Query query, Pageable pageable) {
        query.skip(pageable.getPageNumber());
        query.limit(pageable.getPageSize());
        List<Book> books = mongoTemplate.find(query,Book.class);
        System.out.println();
    }

    public void countBook(Query query) {
        long count = mongoTemplate.count(query,Book.class);
    }

}
